package t;
import java.sql.*;

public class registerBean {
	private String driver = "com.mysql.jdbc.Driver";
	private String url = "jdbc:mysql://localhost:3306/mysql";
	private String user = "root";
	private String passwd = "lf1008";
	private Connection con;
	private ResultSet rs;
	private String res="";
	
	public registerBean(){
		
	}
	public int start(String User,String passwd,String sid){
		if(!getConnect()) return 1;
		if(!checkUser(User,sid)) return 2;
		if(!createUser(User,passwd)) return 3;
		//if(!destroy())return 4;
		return 0;
	}
	
	public String getMessage(){
		return res;
	}
	public void setMessage(){
		this.res = "";
	}
	private boolean getConnect(){
		try
		{
			Class.forName(driver);
			con = DriverManager.getConnection(url,user,passwd);
			return true;
		}
		catch(Exception e){
			res = "获取数据库连接失败"+e.getMessage();
			return false;
		}
	}
	
	public boolean goBack(String User){
		try{
			Statement s = con.createStatement();
			s.execute("drop user '"+User+"'@'%'");
		}
		catch(Exception e){
			res = "清除失败！" + e.getMessage();
		}
		
		return true;
	}
	private boolean checkUser(String name,String sid){
		try{
			PreparedStatement step2 = con.prepareStatement("select User,Host from user where User=?");
			step2.setString(1,name);
			rs = step2.executeQuery();
			if(rs.next()){
				res = "此用户名被抢占啦！请换个名字再试试呀！";
				return false; //用户名已存在
			}
			else			//用户不存在，可以继续往下注册
			{
				Statement sta = con.createStatement();
				sta.execute("use websql");
				ResultSet r =  sta.executeQuery("select * from login where User='"+name+"' or sid='"+sid+"'");
				if(r.next()){
					res =res+ "此用户已存在！可以尝试找回密码！";
					return false;
				}
				else {
					res =res+ "okokokok";
					return true;
				}
			}
		}
		catch(Exception e){
			res =res+ "查询用户信息失败！"+e.getMessage();
			return false;
		}
		
	}
	private boolean createUser(String name,String passwd){
		try{
			String test = "grant all privileges on "+name+".* to "+name+"@'%' identified by '"+passwd+"'";
			Statement prepare = con.createStatement();
			prepare.execute("create database "+name);
			Statement step = con.createStatement();
			step.execute(test);
			return true;
		}
		catch(Exception e){
			try{
				Statement s = con.createStatement();
				//drop user 'jack'@'localhost';
				s.execute("use mysql;");
				s.execute("drop user '"+name+"'@'%'");
				s.execute("drop database "+name);
			}
			catch(Exception x){
				res = "SSSSS！"+x.getMessage();
			}
			res = res+ " 新建注册用户失败！"+e.getMessage();
			return false;
		}
	}
	
	public boolean insertUser(String User,String passwd,String email,String name,String sid){
		try{
			res = "1";
			Statement s = con.createStatement();
			res = "2";
			s.execute("use websql;");
			res = "3";
			PreparedStatement insert= con.prepareStatement("insert into login values(?,?,?,?,?)");
			res = "4";
			insert.setString(1, User);
			insert.setString(2, passwd);
			insert.setString(3, sid);
			insert.setString(4, email);
			insert.setString(5, name);
			res = "5";
			insert.execute();
			res = "6";
		}
		catch(Exception e){
			res = res+ " :插入用户信息失败！"+e.getMessage();
			return false;
		}
		return true;
	}
	public boolean destroy(){
		try{
			return true;
		}
		catch(Exception e){
			res = "关闭数据库连接失败！"+e.getMessage();
			return false;
		}
	}
}
